/*
 * Jenny Ruppen 09-730-391
 * Sebastien Goldberg 08-914-855
 * Henry Raymond 08-928-061
 * Stefan Mussato 09-752-270
 */
 
package ch.uzh.ifi.sopra.gruppe9.game;

import java.util.ArrayList;

import ch.uzh.ifi.sopra.gruppe9.game.TowerElement;


/**
 * Class for built towers made out of tower elements. This class is used by
 * the server and the client. The tower is defined by "instructions", a list of the IDs
 * of its tower elements. This instructions is used by the RMI methods to transmit the 
 * tower information, without transmitting the Tower object.
 */
public class Tower {
	
	
	/**
	 * Constructing instructions
	 */
	private String instructions = "";
	
	
	/**
	 * Material of the tower
	 */
	private TowerElement.Material material = null;
	
	
	/**
	 * Tower elements in the tower
	 */
	private ArrayList<TowerElement> elements = new ArrayList<TowerElement>();
	
	
	/**
	 * Empty constructor. Use assemble() to create tower.
	 */
	public Tower() {
	}
	
	
	/**
	 * Creates tower from a given instruction String. 
	 * Returns false if the tower is not built according to the game's rules.
	 */
	public boolean assemble(String instructions) {
		
		this.instructions = instructions;
		material = null;
		elements.clear();
		
		String[] instructionParts = instructions.split(",");
		
		TowerElement element;
		
		for(int i = 0; i < instructionParts.length; i++) {
			
			element = new TowerElement(Integer.parseInt(instructionParts[i]));
			
			if(i == 0) { // Base
				if(element.getType() != TowerElement.Type.BASE) {
					return false;
				}
				material = element.getMaterial();
				
			} else if(i == (instructionParts.length - 1)) { // Roof
				if(element.getType() != TowerElement.Type.ROOF) {
					return false;
				}
				if(element.getMaterial() != material) {
					return false;
				}
				
			} else { // Middle, window
				if(!(element.getType() == TowerElement.Type.MIDDLE ||
					 element.getType() == TowerElement.Type.WINDOW)) {
					return false;
				}
				if(element.getMaterial() != material) {
					return false;
				}
			}
			elements.add(element);
		}
		
		return true;
	}


	/**
	 * Returns building instructions for tower.
	 */
	public String getInstructions() {
		return instructions;
	}


	/**
	 * Return material of the elements of the tower.
	 */
	public TowerElement.Material getMaterial() {
		return material;
	}
	
	
	/**
	 * Returns the hight of the tower.
	 */
	public int getHeight() {
		return elements.size();
	}

	
	/**
	 * Returns the number of golden elements in the tower.
	 */
	public int getNumOfGolden() {
		int numOfGolden = 0;
		for(int i = 0; i < elements.size(); i++) {
			if(elements.get(i).isGolden()) {
				numOfGolden++;
			}
		}
		return numOfGolden;
	}
	
	
	/**
	 * Checks if tower element with ID is already built in the tower.
	 */
	public boolean isElementInTower(int elementId) {
		for(int i = 0; i < elements.size(); i++) {
			if(elements.get(i).getId() == elementId) {
				return true;
			}
		}
		return false;
	}


	public ArrayList<TowerElement> getElements() {
		return elements;
	}
	
}
